<?php
namespace Admin\Controller;
use Think\Controller;

class FeeController extends \Org\Util\AdminController {
    /**
     * CRUD前后置操作
     */
    public function interfaceBeforeCreate() {
    }
    public function interfaceAfterCreate($id) {

    }
    public function interfaceBeforeDelete($id) {
    }
    public function interfaceAfterDelete($id) {
    }
    public function interfaceBeforeUpdate($id) {
    }
    public function interfaceAfterUpdate($id) {
    }

    /**
     * list页面按钮接口
     * return $html
     */
    public function tableOpeationButton() {
    	return "";
    }
    public function wapTableOpeationButton() {
    	return "";
    }
    
    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id=0) {
        $model=D('fee')->find($id);
              $student=D('student')->where(['id'=>$model['student_id']])->find();
               $class=D('class')->where(['id'=>$student['class_id']])->find();
               $this->assign('class',$class);
               $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
               $this->assign('grade',$grade);
               $major=D('major')->where(['id'=>$class['major_id']])->find();
               $this->assign('major',$major);
               $tuition=D('tuition')->where(['id'=>$model['tuition_id']])->find();
               $term=D('term')->where(['id'=> $tuition['term_id']])->find();
               $this->assign('term',$term);

        $this->assign('model',$model);
        if(IS_POST){
           $tuition=D('tuition')->where(['id'=>$model['tuition_id']])->find();
           D('should')->where(['student_id'=>$student['id'],'tuition_id'=>$tuition['id']])->delete();
            unset($data);
            $data['should']=I('post.should');
            $data['tuition_id']=$tuition['id'];
            $data['student_id']=$student['id'];
            D('should')->add($data);
            $this->success('修改成功',U('Fee/list'));
        }

        $this->display();
    }

    public function list($class_id="",$identity="",$term_name="",$name="") {
        if($class_id!=""){
            $student=D('Student')->where(['class_id'=>$class_id])->select();
            $data=[];
            foreach($student as $k=>$v){
                $data[]=$v['id'];
            }
            if($data){
                $findMap['student_id']=['IN', $data];
            }
        }
        if($identity!=""){
            $student=D('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id']=['EQ',$student['id']];
        }
         if($name!=""){
            $student=D('Student')->where(['name'=>$name])->find();
            $findMap['student_id']=['EQ',$student['id']];
        }
        if($term_name!=""){
            $terms = D('Term')->where(['name'=>['LIKE', '%'.$term_name.'%']])->select();
            $data=[];
            foreach($terms as $term){
                $tuition=D('Tuition')->where(['term_id'=>$term['id']])->select();
                foreach ($tuition as $value){
                    $data[]=$value['id'];
                }
            }
            if(count($data)!=0){
                  $findMap['tuition_id']=['IN',$data];
            }
        }
        $model = M('Fee');
        $count = $model->where($findMap)->count();
        $page = new \Think\AdminPage($count,C('PAGE_NUM'));
        $show = $page->show();
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        if ($term_name !='') {
            $studentId2Value = [];
            foreach($list as $key=>$value) {
                $value['listkey'] = $key;
                $studentId2Value[$value['student_id']][] = $value;
            }
            foreach($studentId2Value  as $student_id=>$value) {
                $should=0;
                $over=0;
                foreach ($value as $k=>$v) {
                    $tuitions=D('tuition')->where(['id'=>$v['tuition_id']])->find();
                    $should=$should+$tuitions['money'];
                    $over=$over+$v['over'];
                    if($k!= 0){
                        unset($list[$v['listkey']]);
                    }else{
                        $key=$v['listkey'];
                    }
                }
                $list[$key]['should']=$should;
                $list[$key]['over']=$over;
                $fee=D('Fee')->where(['student_id'=>$student_id])->select();
                
                foreach($fee as $fees){
                $tuition=D('tuition')->where(['id'=>$fees['tuition_id']])->find();
                //应缴金额
                $should=$tuition['money'];
                //仍欠金额
                $rengqian=$should-$fees['over'];
                $list[$key][$fees['id']]['yingjiao']=$should;
                $list[$key][$fees['id']]['rengqian']=$rengqian;
                }
            }
        }
        
        $this->assign('list',$list);
        // 赋值数据集
        $this->assign('page',$show);
        $this->display();
    }
    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }
}
